package com.yang.utils;

import com.yang.exception.SystemFailException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
@Slf4j
public class RequestUtils {
    /**解析参数*/
    public static String getParam(HttpServletRequest httpRequest){
        try {
            /*获取zipParams参数*/
            ServletInputStream inputStream = httpRequest.getInputStream();
            String param = IOUtils.toString(inputStream);
            inputStream.close();
            return param;
        } catch (IOException e) {
            log.error("解析客户端参数异常",e);
            throw new SystemFailException(e.getMessage());
        }
    }


    /*获取远端Ip*/
    /*获取远端Ip*/
    public static String getRemoteIp(HttpServletRequest request) {
        /**阿里SLB留的坑*/
        String ipAddress = request.getHeader("remoteip");
        if (StringUtils.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("x-forwarded-for");
        }
        if (StringUtils.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (StringUtils.equals(ipAddress,"127.0.0.1")) {
                // 根据网卡取本机配置的IP
                ipAddress = getLocalIp();
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }


    /*根据网卡获取本地ip*/
    public static String getLocalIp(){
        try {
            InetAddress inetAddress= InetAddress.getLocalHost();
            if(inetAddress!=null){
                return inetAddress.getHostAddress();
            }
        } catch (UnknownHostException e) {
            log.error(e.toString());
        }
        return null;
    }


    /**获取header*/
    public static String getHearders(HttpServletRequest request){
        Enumeration enumeration= request.getHeaderNames();
        StringBuilder sb=new StringBuilder();
        while(enumeration.hasMoreElements()){
            String heardName=enumeration.nextElement().toString();
            String value=request.getHeader(heardName);
            sb.append(heardName).append("=").append(value).append(";");
        }
        int index=sb.lastIndexOf(";");
        if (index>0){
            sb.deleteCharAt(index);
        }
        return sb.toString();
    }
}
